* This is the do file to create "Table B1. Summary Statistics (Matched Sample)"
set seed 123

use "$path_data/temp/followup_student_parents_matched", clear

gen gend = q1d - 1

local controls DT_score_pre_std_missing_0 rosen_pre_std_missing_0 cpcs_pre_std_missing_0 i.grade gend branch1 branch2 branch3 income_source1 income_source2 income_source3 income_source4 last_income_per_member hhmember hhheadage hhheadeduyear phone_survey age_tchr
teffects psmatch (followup_cog_std) (treatment `controls') 
psmatch2 treatment `controls', outcome(followup_cog_std) noreplacement
gen psmattrition = 1 if _support!=1
recode psmattrition (.=0)
keep if psmattrition == 0

/// Varable Selection
/// Baseline
foreach j in n mean sd {
tabstat DT_score_pre_std rosen_pre_std cpcs_pre_std if treatment == 1, stat(`j') save
matrix list r(StatTotal)
matrix `j'_tr_bl = r(StatTotal)

tabstat DT_score_pre_std rosen_pre_std cpcs_pre_std if treatment == 0, stat(`j') save
matrix list r(StatTotal)
matrix `j'_ct_bl = r(StatTotal)
}

matrix n_bl = J(1,3,.)
forvalues i = 1/3 {
	matrix n_bl[1,`i'] = n_tr_bl[1,`i'] + n_ct_bl[1,`i']
}

foreach dep in DT_score_pre_std rosen_pre_std cpcs_pre_std{
    
    wildbootstrap reg `dep' treatment, cluster(school_no) reps(1000)
	
	matrix r2_`dep'_temp = r(table)


    foreach s in mean se pv obs {
		matrix r2_`dep'_`s' = J(1,2,.)
	}

	foreach j in 1 2 {
	* beta
	matrix r2_`dep'_mean[1,`j'] = r2_`dep'_temp[1,`j']
	* standard error
	* matrix r2_`dep'_se[1,`j'] = r2_`dep'_temp[2,`j']
	* p value
	matrix r2_`dep'_pv[1,`j'] = r2_`dep'_temp[3,`j']
	}

}

/// Family
foreach j in n mean sd {
tabstat hhmember hhheadage hhheadeduyear if treatment == 1, stat(`j') save
matrix list r(StatTotal)
matrix `j'_tr_parent = r(StatTotal)

tabstat hhmember hhheadage hhheadeduyear if treatment == 0, stat(`j') save
matrix list r(StatTotal)
matrix `j'_ct_parent = r(StatTotal)
}

matrix n_parent = J(1,3,.)
forvalues i = 1/3 {
	matrix n_parent[1,`i'] = n_tr_parent[1,`i'] + n_ct_parent[1,`i']
}

foreach dep in hhmember hhheadage hhheadeduyear{
    
    wildbootstrap reg `dep' treatment, cluster(school_no) reps(1000)
	
	matrix r2_`dep'_temp = r(table)


    foreach s in mean se pv obs {
		matrix r2_`dep'_`s' = J(1,2,.)
	}

	foreach j in 1 2 {
	* beta
	matrix r2_`dep'_mean[1,`j'] = r2_`dep'_temp[1,`j']
	* standard error
	* matrix r2_`dep'_se[1,`j'] = r2_`dep'_temp[2,`j']
	* p value
	matrix r2_`dep'_pv[1,`j'] = r2_`dep'_temp[3,`j']
	}

}



/// School　attendance
foreach j in n mean sd {
tabstat q2a q2b q2c q2h if treatment == 1, stat(`j') save
matrix list r(StatTotal)
matrix `j'_tr_school = r(StatTotal)

tabstat q2a q2b q2c q2h if treatment == 0, stat(`j') save
matrix list r(StatTotal)
matrix `j'_ct_school = r(StatTotal)
}

matrix n_school = J(1,4,.)
forvalues i = 1/4 {
	matrix n_school[1,`i'] = n_tr_school[1,`i'] + n_ct_school[1,`i']
}

foreach dep in q2a q2b q2c q2h{
    
    wildbootstrap reg `dep' treatment, cluster(school_no) reps(1000)
	
	matrix r2_`dep'_temp = r(table)


    foreach s in mean se pv obs {
		matrix r2_`dep'_`s' = J(1,2,.)
	}

	foreach j in 1 2 {
	* beta
	matrix r2_`dep'_mean[1,`j'] = r2_`dep'_temp[1,`j']
	* standard error
	* matrix r2_`dep'_se[1,`j'] = r2_`dep'_temp[2,`j']
	* p value
	matrix r2_`dep'_pv[1,`j'] = r2_`dep'_temp[3,`j']
	}

}

/// Other study variable
foreach j in n mean sd {
tabstat tutor study_other study_affect_covid hometutoring onlineclass studymyself parentsteach if treatment == 1, stat(`j') save
matrix list r(StatTotal)
matrix `j'_tr_study = r(StatTotal)

tabstat tutor study_other study_affect_covid hometutoring onlineclass studymyself parentsteach if treatment == 0, stat(`j') save
matrix list r(StatTotal)
matrix `j'_ct_study = r(StatTotal)
}

matrix n_study = J(1,8,.)
forvalues i = 1/8 {
	matrix n_study[1,`i'] = n_tr_study[1,`i'] + n_ct_study[1,`i']
}

foreach dep in tutor study_other study_affect_covid hometutoring onlineclass studymyself parentsteach{
    
    wildbootstrap reg `dep' treatment, cluster(school_no) reps(1000)
	
	matrix r2_`dep'_temp = r(table)


    foreach s in mean se pv obs {
		matrix r2_`dep'_`s' = J(1,2,.)
	}

	foreach j in 1 2 {
	* beta
	matrix r2_`dep'_mean[1,`j'] = r2_`dep'_temp[1,`j']
	* standard error
	* matrix r2_`dep'_se[1,`j'] = r2_`dep'_temp[2,`j']
	* p value
	matrix r2_`dep'_pv[1,`j'] = r2_`dep'_temp[3,`j']
	}

}

/// Cognitive
foreach j in n mean sd {
tabstat followup_cog_std if treatment == 1, stat(`j') save
matrix list r(StatTotal)
matrix `j'_tr_cog = r(StatTotal)

tabstat followup_cog_std if treatment == 0, stat(`j') save
matrix list r(StatTotal)
matrix `j'_ct_cog = r(StatTotal)
}

matrix n_cog = J(1,1,.)
forvalues i = 1/1 {
	matrix n_cog[1,`i'] = n_tr_cog[1,`i'] + n_ct_cog[1,`i']
}

wildbootstrap reg followup_cog_std treatment, cluster(school_no) reps(1000)
	
matrix r2_followup_cog_std_temp = r(table)


foreach s in mean se pv obs {
		matrix r2_followup_cog_std_`s' = J(1,2,.)
}

foreach j in 1 2 {
* beta
matrix r2_followup_cog_std_mean[1,`j'] = r2_followup_cog_std_temp[1,`j']
* standard error
* matrix r2_followup_cog_std_se[1,`j'] = r2_followup_cog_std_temp[2,`j']
* p value
matrix r2_followup_cog_std_pv[1,`j'] = r2_followup_cog_std_temp[3,`j']
}
    
/// Non cognitive
foreach j in n mean sd {
tabstat followup_noncog_std RSES_std CPCS_std if treatment == 1, stat(`j') save
matrix list r(StatTotal)
matrix `j'_tr_noncog = r(StatTotal)

tabstat followup_noncog_std RSES_std CPCS_std if treatment == 0, stat(`j') save
matrix list r(StatTotal)
matrix `j'_ct_noncog = r(StatTotal)
}

matrix n_noncog = J(1,3,.)
forvalues i = 1/3 {
	matrix n_noncog[1,`i'] = n_tr_noncog[1,`i'] + n_ct_noncog[1,`i']
}

foreach dep in followup_noncog_std RSES_std CPCS_std{
    
    wildbootstrap reg `dep' treatment, cluster(school_no) reps(1000)
	
	matrix r2_`dep'_temp = r(table)


    foreach s in mean se pv obs {
		matrix r2_`dep'_`s' = J(1,2,.)
	}

	foreach j in 1 2 {
	* beta
	matrix r2_`dep'_mean[1,`j'] = r2_`dep'_temp[1,`j']
	* standard error
	* matrix r2_`dep'_se[1,`j'] = r2_`dep'_temp[2,`j']
	* p value
	matrix r2_`dep'_pv[1,`j'] = r2_`dep'_temp[3,`j']
	}

}


/// Behavioral
foreach j in n mean sd {
tabstat hyper if treatment == 1, stat(`j') save
matrix list r(StatTotal)
matrix `j'_tr_hyper = r(StatTotal)

tabstat hyper if treatment == 0, stat(`j') save
matrix list r(StatTotal)
matrix `j'_ct_hyper = r(StatTotal)
}

matrix n_hyper = J(1,1,.)
forvalues i = 1/1 {
	matrix n_hyper[1,`i'] = n_tr_hyper[1,`i'] + n_ct_hyper[1,`i']
}

foreach dep in hyper{
    
    wildbootstrap reg `dep' treatment if hypernoinfo == 0, cluster(school_no) reps(1000)
	
	matrix r2_`dep'_temp = r(table)


    foreach s in mean se pv obs {
		matrix r2_`dep'_`s' = J(1,2,.)
	}

	foreach j in 1 2 {
	* beta
	matrix r2_`dep'_mean[1,`j'] = r2_`dep'_temp[1,`j']
	* standard error
	* matrix r2_`dep'_se[1,`j'] = r2_`dep'_temp[2,`j']
	* p value
	matrix r2_`dep'_pv[1,`j'] = r2_`dep'_temp[3,`j']
	}

}


// significant level

local outcome DT_score_pre_std rosen_pre_std cpcs_pre_std hhmember hhheadage hhheadeduyear q2a q2c q2h tutor study_other followup_cog_std RSES_std CPCS_std

foreach dep in `outcome'{
		if r2_`dep'_pv[1,1]<=0.01 {
			local star_`dep' %3s "***"
		}
		else if (r2_`dep'_pv[1,1]>0.01) & (r2_`dep'_pv[1,1]<=0.05) {
			local star_`dep' %2s "**"
		}
		else if (r2_`dep'_pv[1,1]>0.05) & (r2_`dep'_pv[1,1]<=0.10) {
			local star_`dep' %1s "*"
		}
		else {
			local star_`dep'  ""
		}
} 

rwolf DT_score_pre_std rosen_pre_std cpcs_pre_std hhmember hhheadage hhheadeduyear, indepvar(treatment) reps(1000)
rwolf q2a q2c q2h tutor study_other followup_cog_std RSES_std CPCS_std, indepvar(treatment) reps(1000)




/// Table
tempname hh2
file open `hh2' using "$path_output/summary_stat_psmatch.tex", write replace
file write `hh2' "" _newline
file write `hh2' "% Author: Kazuma Takakura" _newline
file write `hh2' "% Date: `c(current_date)'" _newline
file write `hh2' "% Time: `c(current_time)'" _newline
file write `hh2' "" _newline


file write `hh2' "\begin{table}[h!]\footnotesize" _newline
file write `hh2' "  \centering" _newline
file write `hh2' "  \caption{Summary Statistics (Matched Sample)}" _newline
file write `hh2' "\label{tab:sumstat_psm}" _newline
file write `hh2' "\scalebox{1}{" _newline
file write `hh2' "\begin{threeparttable}" _newline

file write `hh2' "\begin{tabular}{lccccc}\toprule" _newline

  
file write `hh2' " Dependent Variable & Treatment &  Control  & Difference & N   \\\midrule\midrule" _newline
file write `hh2' " Panel A: Baseline & & & &   \\ " _newline
file write `hh2' " DT score^{a} & " %04.3f (mean_tr_bl[1,1]) " & " %04.3f (mean_ct_bl[1,1]) " & " %04.3f (r2_DT_score_pre_std_mean[1,1]) `star_DT_score_pre_std' " & " (n_bl[1,1]) "  \\ " _newline
file write `hh2' "    				 & [ " %04.3f (sd_tr_bl[1,1]) " ] & [ " %04.3f (sd_ct_bl[1,1]) " ] & ( " %04.3f (r2_DT_score_pre_std_pv[1,1]) " ) &   \\ " _newline
file write `hh2' "    				 &   &   & \{ " %04.3f (0.893) " \} &   \\ " _newline


file write `hh2' " RSES score^{a} & " %04.3f (mean_tr_bl[1,2]) " & " %04.3f (mean_ct_bl[1,2]) " & " %04.3f (r2_rosen_pre_std_mean[1,1]) `star_rosen_pre_std' " & "  (n_bl[1,2]) " \\ " _newline
file write `hh2' "    				 & [ " %04.3f (sd_tr_bl[1,2]) " ] & [ " %04.3f (sd_ct_bl[1,2]) " ] & ( " %04.3f (r2_rosen_pre_std_pv[1,1]) " ) &   \\ " _newline
file write `hh2' "    				 &   &   & \{ " %04.3f (0.948) " \} &   \\ " _newline

file write `hh2' " CPCS score^{a} & " %04.3f (mean_tr_bl[1,3]) " & " %04.3f (mean_ct_bl[1,3]) " & " %04.3f (r2_cpcs_pre_std_mean[1,1]) `star_cpcs_pre_std' " & "  (n_bl[1,3]) " \\ " _newline
file write `hh2' "    				 & [ " %04.3f (sd_tr_bl[1,3]) " ] & [ " %04.3f (sd_ct_bl[1,3]) " ] & ( " %04.3f (r2_cpcs_pre_std_pv[1,1]) " ) &   \\ " _newline
file write `hh2' "    				 &   &   & \{ " %04.3f (0.806) " \} &   \\ " _newline

file write `hh2' " Household size & " %04.3f (mean_tr_parent[1,1]) " & " %04.3f (mean_ct_parent[1,1]) " & " %04.3f (r2_hhmember_mean[1,1]) `star_hhmember'  " & " (n_parent[1,1]) "  \\ " _newline
file write `hh2' "    				 & [ " %04.3f (sd_tr_parent[1,1]) " ] & [ " %04.3f (sd_ct_parent[1,1]) " ] & ( " %04.3f (r2_hhmember_pv[1,1]) " ) &   \\ " _newline
file write `hh2' "    				 &   &   & \{ " %04.3f (0.948) " \} &   \\ " _newline

file write `hh2' " Household head age & " %04.3f (mean_tr_parent[1,2]) " & " %04.3f (mean_ct_parent[1,2]) " & " %04.3f (r2_hhheadage_mean[1,1]) `star_hhheadage' " & "  (n_parent[1,2]) " \\ " _newline
file write `hh2' "    				 & [ " %04.3f (sd_tr_parent[1,2]) " ] & [ " %04.3f (sd_ct_parent[1,2]) " ] & ( " %04.3f (r2_hhheadage_pv[1,1]) " ) &   \\ " _newline
file write `hh2' "    				 &   &   & \{ " %04.3f (0.948) " \} &   \\ " _newline

file write `hh2' " Household head education & " %04.3f (mean_tr_parent[1,3]) " & " %04.3f (mean_ct_parent[1,3]) " & " %04.3f (r2_hhheadeduyear_mean[1,1]) `star_hhheadeduyear' " & "  (n_parent[1,3]) "  \\ " _newline
file write `hh2' "    				 & [ " %04.3f (sd_tr_parent[1,3]) " ] & [ " %04.3f (sd_ct_parent[1,3]) " ] & ( " %04.3f (r2_hhheadeduyear_pv[1,1]) " ) &   \\ " _newline
file write `hh2' "    				 &   &   & \{ " %04.3f (0.893) " \} &   \\ " _newline
file write `hh2' " \\ "_newline

file write `hh2' " Panel B: Follow-up & & & &   \\ " _newline

file write `hh2' " School attendance & " %04.3f (mean_tr_school[1,1]) " & " %04.3f (mean_ct_school[1,1]) " & " %04.3f (r2_q2a_mean[1,1]) `star_q2a' " & " (n_school[1,1]) "  \\ " _newline
file write `hh2' "    				 & [ " %04.3f (sd_tr_school[1,1]) " ] & [ " %04.3f (sd_ct_school[1,1]) " ] & ( " %04.3f (r2_q2a_pv[1,1]) " ) &   \\ " _newline
file write `hh2' "    				 &   &   & \{ " %04.3f (0.818) " \} &   \\ " _newline

file write `hh2' " Grade repeat & " %04.3f (mean_tr_school[1,3]) " & " %04.3f (mean_ct_school[1,3]) " & " %04.3f (r2_q2c_mean[1,1]) `star_q2c' " & "  (n_school[1,3]) " \\ " _newline
file write `hh2' "    				 & [ " %04.3f (sd_tr_school[1,3]) " ] & [ " %04.3f (sd_ct_school[1,3]) " ] & ( " %04.3f (r2_q2c_pv[1,1]) " ) &   \\ " _newline
file write `hh2' "    				 &   &   & \{ " %04.3f (0.818) " \} &   \\ " _newline

file write `hh2' " Drop out & " %04.3f (mean_tr_school[1,4]) " & " %04.3f (mean_ct_school[1,4]) " & " %04.3f (r2_q2h_mean[1,1]) `star_q2h'  " & "  (n_school[1,4]) "  \\ " _newline
file write `hh2' "    				 & [ " %04.3f (sd_tr_school[1,4]) " ] & [ " %04.3f (sd_ct_school[1,4]) " ] & ( " %04.3f (r2_q2h_pv[1,1]) " ) &   \\ " _newline
file write `hh2' "    				 &   &   & \{ " %04.3f (0.637) " \} &   \\ " _newline

file write `hh2' " Tutoring & " %04.3f (mean_tr_study[1,1]) " & " %04.3f (mean_ct_study[1,1]) " & " %04.3f (r2_tutor_mean[1,1]) `star_tutor'  " & " (n_study[1,1]) "  \\ " _newline
file write `hh2' "    				 & [ " %04.3f (sd_tr_study[1,1]) " ] & [ " %04.3f (sd_ct_study[1,1]) " ] & ( " %04.3f (r2_tutor_pv[1,1]) " ) &   \\ " _newline
file write `hh2' "    				 &   &   & \{ " %04.3f (0.637) " \} &   \\ " _newline

file write `hh2' " Self-study & " %04.3f (mean_tr_study[1,2]) " & " %04.3f (mean_ct_study[1,2]) " & " %04.3f (r2_study_other_mean[1,1]) `star_study_other' " & "  (n_study[1,2]) " \\ " _newline
file write `hh2' "    				 & [ " %04.3f (sd_tr_study[1,2]) " ] & [ " %04.3f (sd_ct_study[1,2]) " ] & ( " %04.3f (r2_study_other_pv[1,1]) " ) &   \\ " _newline
file write `hh2' "    				 &   &   & \{ " %04.3f (0.811) " \} &   \\ " _newline

file write `hh2' " Rapid math test score^{a} & " %04.3f (mean_tr_cog[1,1]) " & " %04.3f (mean_ct_cog[1,1]) " & " %04.3f (r2_followup_cog_std_mean[1,1]) `star_followup_cog_std'  "  & "  (n_cog[1,1]) "  \\ " _newline
file write `hh2' "    				 & [ " %04.3f (sd_tr_cog[1,1]) " ] & [ " %04.3f (sd_ct_cog[1,1]) " ] & ( " %04.3f (r2_followup_cog_std_pv[1,1]) " ) &   \\ " _newline
file write `hh2' "    				 &   &   & \{ " %04.3f (0.203) " \} &   \\ " _newline

file write `hh2' " RSES score^{a} & " %04.3f (mean_tr_noncog[1,2]) " & " %04.3f (mean_ct_noncog[1,2]) " & " %04.3f (r2_RSES_std_mean[1,1])   `star_RSES_std' " & " (n_noncog[1,2]) "  \\ " _newline
file write `hh2' "    				 & [ " %04.3f (sd_tr_noncog[1,2]) " ] & [ " %04.3f (sd_ct_noncog[1,2]) " ] & ( " %04.3f (r2_RSES_std_pv[1,1]) " ) &   \\ " _newline
file write `hh2' "    				 &   &   & \{ " %04.3f (0.002) " \} &   \\ " _newline

file write `hh2' " CPCS score^{a} & " %04.3f (mean_tr_noncog[1,3]) " & " %04.3f (mean_ct_noncog[1,3]) " & " %04.3f (r2_CPCS_std_mean[1,1])   `star_CPCS_std' "&  " (n_noncog[1,3]) " \\ " _newline
file write `hh2' "    				 & [ " %04.3f (sd_tr_noncog[1,3]) " ] & [ " %04.3f (sd_ct_noncog[1,3]) " ] & ( " %04.3f (r2_CPCS_std_pv[1,1]) " ) &   \\ " _newline
file write `hh2' "    				 &   &   & \{ " %04.3f (0.001) " \} &   \\ " _newline




file write `hh2' "\midrule" _newline
file write `hh2' "\end{tabular}" _newline
file write `hh2' "\begin{tablenotes}" _newline
file write `hh2' "\item (a) Standardized in the follow-up sample. " _newline
file write `hh2' "\item (b) For the propensity score matching, we use covariates including student's grade, sex, branch dummy (location), parents' income source, last income per family member, number of household members, age of household head, education level of household head, teacher's age, sex, and phone survey dummy." _newline
file write `hh2' "\item (c) Standard deviations are reported in square brackets. Standard errors, which are reported within parentheses are clustered by school at the baseline. " _newline
file write `hh2' "\item (d) Romano-Wolf multiple hypothesis testing p-values are reported in curly brackets." _newline
file write `hh2' "Statistical significance is indicated by stars based on the wild clustered bootstrap p-values reported in parentheses: $*$ denotes significance at the 10\% level, $∗∗$ at the 5\% level, and $∗∗∗$ at the 1\% level. " _newline
file write `hh2' "\end{tablenotes}" _newline
file write `hh2' "\end{threeparttable}" _newline
file write `hh2' "}" _newline
file write `hh2' "\label{tab:addlabel}%" _newline
file write `hh2' "\end{table}" _newline

file write `hh2' "" _newline
file write `hh2' "" _newline
file write `hh2' "" _newline
file write `hh2' "" _newline

file close `hh2'
